<template>
<div>
  <div>
    <el-input style="width: 200px;"  placeholder="请输入名称" v-model="name" suffix-icon="el-icon-search"></el-input>
    <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
    <el-button @click="reset" type="warning">重置</el-button>
  </div>
    <div style="margin:6px 0">
    <el-upload :action="'http://'+serverIp+':9090/file/upload'"
               :show-file-list="false"
               :on-success="handleFileUploadSuccess"
               style="display: inline-block">
      <el-button type="primary" >上传文件<i class="el-icon-top"/> </el-button>
    </el-upload>
    <el-popconfirm  title="确定批量删除吗？" @confirm="delBatch"
                    class="ml-5">
      <el-button type="danger" slot="reference">批量删除
        <i class="el-icon-remove-outline" />
      </el-button>
    </el-popconfirm>
  </div>
  <el-table :data="tableData" border stripe :header-cell-style="tableHeaderColor" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="id" label="ID" width="100"></el-table-column>
    <el-table-column prop="name" label="文件名称" ></el-table-column>
    <el-table-column prop="type" label="文件类型" ></el-table-column>
    <el-table-column prop="md5" label="md5" width="300" ></el-table-column>
    <el-table-column prop="size" label="文件大小(kb)"></el-table-column>
    <el-table-column  label="下载">
      <template slot-scope="scope">
        <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
      </template>
    </el-table-column>
    <el-table-column  label="启用">
      <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc" @change="changeEnable(scope.row)"></el-switch>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="200" align="center">
      <template slot-scope="scope">
        <el-popconfirm  title="确定删除吗？" @confirm="del(scope.row.id)"
                        class="ml-5">
          <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>

      </template>
    </el-table-column>

  </el-table>
  <div style="padding:10px 0">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</div>
</template>

<script>
import {serverIp} from "../../public/config";

export default {
  name: "File",
  data(){
    return{
      serverIp:serverIp,
      tableData:[],
      name:'',
      multipleSelection:[],
      pageNum:1,
      pageSize:10,
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods:{
    handleEdit(row){
      this.form=row
      this.dialogFormVisible=true
    },
    reset(){
      this.name=""
      this.load()
    },
    handleSelectionChange(val){
      console.log(val)
      this.multipleSelection=val
    },
    delBatch(){
      let ids=this.multipleSelection.map(v=>v.id)  //[ {},{},{}] => [1,2,3]
      this.request.post("/file/del/batch",ids).then(res=>{
        if(res.code==='200'){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error("批量删除失败")
        }
      })
    },
    load(){
      this.request.get("/file/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          name:this.name
        }
      }).then(res =>{
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    search(){
      this.request.get("/file/page",{
        params:{
          pageNum:1,
          pageSize:this.pageSize,
          name:this.name
        }
      }).then(res =>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    changeEnable(row){
      this.request.post("/file/update",row).then(res=>{
        if(res.code==='200'){
          this.$message.success("操作成功")
        }
      })
    },
    del(id){
      this.request.delete("/file/"+id).then(res=>{
        if(res.code==='200'){
          this.$message.success("删除成功")
          this.load()
        }else{
          this.$message.error("删除失败")
        }
      })
    },
    save(){ //新增或修改
      this.request.post("/file",this.form).then(res =>{
        if(res.data){
          this.$message.success("保存成功")
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("保存失败")
          this.load()
        }
      })

    },
    cancel(){
      this.dialogFormVisible=false
      this.load()
    },

    tableHeaderColor({row,column,rowIndex,columnIndex}){
      return 'background-color:lightblue;color:#fff;font-size:15px;'
    },
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum=pageNum;
      this.load()
    },
    handleFileUploadSuccess(res){
      console.log(res)
      this.load()
    },
    download(url){
      window.open(url)
    }
  }

}
</script>

<style scoped>

</style>